'''
Created on Sep 17, 2011

@author: hp
'''
import urllib
import urllib2
import json

def showsome(searchfor):
    hits = -1
    try:
            query = urllib.urlencode({'q': searchfor})
            url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
            search_response = urllib.urlopen(url)
            print 'hehe'
            search_results = search_response.read()
            print 'hichi'
            results = json.loads(search_results)
            print 'huhu'
            data = results['responseData']
            print 'cai gi'
            print 'data', data
            if data['cursor'].has_key('estimatedResultCount'):
                hits = int(data['cursor']['estimatedResultCount'])
    except:
            #error.write(formatExceptionInfo())
            print 'error!'
            
    return hits

def get_result(query=''):
    """
    """
    the_url = 'http://ajax.googleapis.com/ajax/services/search/web'
    the_data = urllib.urlencode({'v':'1.0', 'q':'Google', 'key':'AIzaSyDUBedYetF9kpG-TXaILJq_AFGB9jkt1ZY'})
    the_header = {'User-agent': 'Mozilla/5.0'}
    
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    http_request = urllib2.Request(the_url, the_data, the_header)
    http_handle = opener.open(http_request)
    content = http_handle.read()
    http_handle.close()
    
    print content
    
showsome('"Ahmedinejad on attacks by"')